package com.sdp.right.db.sys_flow_task_his.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.right.db.sys_flow_task_his.mapper.SysFlowTaskHisMapper;
import com.sdp.right.db.sys_flow_task_his.service.ISysFlowTaskHisService;
import com.sdp.right.entity.SysFlowTaskHis;
import com.sdp.right.service.RemoveSystemService;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-07-18
 */
@Service
public class SysFlowTaskHisServiceImpl extends ServiceImpl<SysFlowTaskHisMapper, SysFlowTaskHis> implements ISysFlowTaskHisService,RemoveSystemService {

	

	@Override
	public void clearFlowTaskHisVersion(String version, String templateNo) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.ne(SysFlowTaskHis::getTemplateVersion, version);
		queryWrapper.eq(SysFlowTaskHis::getTemplateNo, templateNo);
		remove(queryWrapper);
		
	}

	@Override
	public void removeApprovalTaskHis(String instanceId, String nodeId) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowTaskHis::getInstanceId, instanceId);
		queryWrapper.eq(SysFlowTaskHis::getCurrentNodeId, nodeId);
		queryWrapper.eq(SysFlowTaskHis::getApprovalState, RightConstants.流程审批状态_审批中.getCode());
		remove(queryWrapper);
		
	}

	@Override
	public List<SysFlowTaskHis> queryApprovalFlowTaskHis(String instanceId, String nodeId,String approvalState) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowTaskHis::getInstanceId, instanceId);
		queryWrapper.eq(SysFlowTaskHis::getCurrentNodeId, nodeId);
		queryWrapper.eq(SysFlowTaskHis::getApprovalState, approvalState);
		return list(queryWrapper);
	}

	@Override
	public List<SysFlowTaskHis> queryLastApprovalFlowTaskHis(String instanceId, String nodeId) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowTaskHis::getInstanceId, instanceId);
		queryWrapper.eq(SysFlowTaskHis::getCurrentNodeId, nodeId);
		queryWrapper.isNotNull(SysFlowTaskHis::getApprovalTime);
		queryWrapper.orderByDesc(SysFlowTaskHis::getApprovalTime);
		return list(queryWrapper);
	}

	@Override
	public List<SysFlowTaskHis> queryapprovedFlowTaskHis(String instanceId) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowTaskHis::getInstanceId, instanceId);
		queryWrapper.ne(SysFlowTaskHis::getApprovalState, RightConstants.流程审批状态_审批中.getCode());
		queryWrapper.orderByDesc(SysFlowTaskHis::getApprovalTime);
		return list(queryWrapper);
	}

	@Override
	public void removeBySystemNo(String systemNo) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowTaskHis::getSystemNo, systemNo);
		remove(queryWrapper);
		
		
	}

	@Override
	public void removeFlowTaskHisByInstanceId(String instanceId) {
		LambdaQueryWrapper<SysFlowTaskHis> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowTaskHis::getInstanceId, instanceId);
		remove(queryWrapper);
		
	}

}
